package com.example.test.rke;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import java.util.Queue;

public class WgUdpCommShortHandler implements IoHandler {

	public Queue<byte[]> queue;

	public WgUdpCommShortHandler(Queue<byte[]> queue) {
		super();
		this.queue = queue;
	}

	public void exceptionCaught(IoSession session, Throwable e) throws Exception {
		e.printStackTrace();
		session.close(true);
	}

	public void messageReceived(IoSession session, Object message) throws Exception {
		IoBuffer io = (IoBuffer) message;
		if (io.hasRemaining()) {
			byte[] validBytes = new byte[io.remaining()];
			io.get(validBytes, 0, io.remaining());
			if ((validBytes.length == WgUDPUtil.COMMAND_PACKET_LENGTH) && (validBytes[0] == WgUDPUtil.TYPE)) {
				queue.offer(validBytes);
			}
		}
	}

	public void messageSent(IoSession session, Object message) throws Exception {
	}

	public void sessionClosed(IoSession session) throws Exception {
	}

	public void sessionCreated(IoSession session) throws Exception {
	}

	public void sessionIdle(IoSession session, IdleStatus idle) throws Exception {
	}

	public void sessionOpened(IoSession session) throws Exception {
	}

}
